package cn.lsc.system.controller;

import cn.lsc.basic.controller.util.JsonResult;
import cn.lsc.basic.controller.util.PageList;

import cn.lsc.system.domain.Systemdictionarydetail;
import cn.lsc.system.domain.Systemdictionarytype;
import cn.lsc.system.query.SystemdictionarytypeQuery;

import cn.lsc.system.service.ISystemdictionarydetailService;
import cn.lsc.system.service.ISystemdictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/systemdictionarytype")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class SystemdictionarytypeController {
    @Autowired
    private ISystemdictionarytypeService ISystemdictionarytypeService;


    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;


    /*
    * 查询所有
    * */
    @GetMapping
    public List<Systemdictionarytype> findAll(){
       return ISystemdictionarytypeService.findAll();
    }


    /*
    * 查询一个
    * */
    @GetMapping("/{id}")
    public Systemdictionarytype findById(@PathVariable("id") Long id){
        return ISystemdictionarytypeService.findById(id);
    }

    /*
    * 修改和添加操作
    * */

    @PutMapping
    public JsonResult UpdateOrAdd(@RequestBody Systemdictionarytype Systemdictionarytype){
        try {
            if(Systemdictionarytype.getId()==null){
                ISystemdictionarytypeService.add(Systemdictionarytype);
            }else {
                ISystemdictionarytypeService.update(Systemdictionarytype);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    /*
    * 删除操作
    * */

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            List<Systemdictionarydetail> all = systemdictionarydetailService.findAll();
            for (Systemdictionarydetail systemdictionarydetail : all) {
                if(systemdictionarydetail.getTypes_id()==id){
                    Long id1 = systemdictionarydetail.getId();

                    systemdictionarydetailService.del(id1);
                }
            }
            ISystemdictionarytypeService.del(id);
            return JsonResult.me();
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    /*
    * 分页查询
    * */

    @PostMapping
    public PageList<Systemdictionarytype> pageList(@RequestBody SystemdictionarytypeQuery query){
        try {
           return ISystemdictionarytypeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            for (Long id : ids) {
                List<Systemdictionarydetail> all = systemdictionarydetailService.findAll();
                for (Systemdictionarydetail systemdictionarydetail : all) {
                    if (systemdictionarydetail.getTypes_id() == id) {
                        Long id1 = systemdictionarydetail.getId();

                        systemdictionarydetailService.del(id1);
                    }
                }
            }
            ISystemdictionarytypeService.patchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


}
